﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;

namespace Woodlands
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Page
    {
        MainWindow main;
        Staff staff;
        public LoginWindow()
        {
            InitializeComponent();
            double left = (System.Windows.SystemParameters.PrimaryScreenWidth/2) - 150;
            LoginGB.Margin = new Thickness(left, 100, 0, 0);
        }
        public void setParent(MainWindow x, Staff y)
        {
            main = x;
            staff = y;
        }
        private async void Login_Click(object sender, RoutedEventArgs e)
        {
            Login.Content = "Loading...";
            Login.IsEnabled = false;
            Boolean result = await Database.login(UsernameBox.Text, PasswordBox.Password,staff);
            if (result)
            {
                main.Login_Successful();
                staff.ID = UsernameBox.Text;
                UsernameBox.Text = "";
                PasswordBox.Password = "";
            }
            else
            {
                System.Media.SystemSounds.Asterisk.Play();
                PasswordBox.Password = "";
            }
            
            Login.Content = "Login";
            Login.IsEnabled = true;
        }

        private async void PasswordBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (Key.Return == e.Key)
            {
                Login.Content = "Loading...";
                Login.IsEnabled = false;
                Boolean result;
                result = await Database.login(UsernameBox.Text, PasswordBox.Password, staff);
                if (result)
                {
                    main.Login_Successful();
                    staff.ID = UsernameBox.Text;
                    UsernameBox.Text = "";
                    PasswordBox.Password = "";
                }
                else
                {
                    System.Media.SystemSounds.Asterisk.Play();
                    PasswordBox.Password = "";
                }
                Login.Content = "Login";
                Login.IsEnabled = true;
            }
        }

    }
}
